package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.exception.BizCodeException;
import com.atguigu.common.exception.RRException;
import com.atguigu.common.to.MemberTo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberLevelService;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.gulimall.member.vo.MemberVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberVO memberVO) {
        // 对拷属性
        MemberEntity member = new MemberEntity();
        member.setUsername(memberVO.getUsername());
        // 对用户密码进行MD5盐值加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(memberVO.getPassword());
        member.setPassword(encode);
        member.setMobile(memberVO.getPhone());
        member.setLevelId(memberLevelService.getDefaultLevel());

        // 校验用户名和手机号的唯一性
        checkUsernameUnique(member.getUsername());
        checkPhoneUnique(member.getMobile());

        this.save(member);
    }

    @Override
    public MemberTo login(MemberVO memberVO) {
        // 通过用户名或手机号查询用户信息
        MemberEntity member = this.getOne(new QueryWrapper<MemberEntity>().eq("username", memberVO.getUsername()).or().eq("mobile", memberVO.getUsername()));

        // 校验密码是否正确
        if (member != null) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            if (passwordEncoder.matches(memberVO.getPassword(), member.getPassword())) {
                MemberTo memberTo = new MemberTo();
                BeanUtils.copyProperties(member, memberTo);
                return memberTo;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    private void checkPhoneUnique(String mobile) throws RRException {
        int count = this.count(new QueryWrapper<MemberEntity>().eq("mobile", mobile));
        if (count > 0) {
            throw new RRException(BizCodeException.PHONE_EXIT_EXCEPTION.getMsg(), BizCodeException.PHONE_EXIT_EXCEPTION.getCode());
        }
    }

    private void checkUsernameUnique(String username) throws RRException {
        int count = this.count(new QueryWrapper<MemberEntity>().eq("username", username));
        if (count > 0) {
            throw new RRException(BizCodeException.USERNAME_EXIT_EXCEPTION.getMsg(), BizCodeException.USERNAME_EXIT_EXCEPTION.getCode());
        }
    }

}